#ifndef CDN_PWM_H
#define CDN_PWM_H
/*
*******************************************************************************
** Copyright (C), 2014-2020, TONLY AV R&D CENTER SOFTWARE DEVELOPMENT DEPARTMENT
** FileName: 
** <author>:             
** Date:
** Description:
**
** Others:
** Function List:
**   1.
** History:
**  <Author>    <date>    <content>    <version>
*******************************************************************************
*/
/*******************************************************************************
 * file include
*******************************************************************************/
#include "stdint.h"
#include "sl_pwm.h"
#include "cdn_light.h"

/*******************************************************************************
 * local define
*******************************************************************************/
#define CDN_MAX_POWER_12W_24W       1         //12W/24W驱动器选择，0---12W，1---24W
#define ZZX_Test       0//1-----DIO8280的亮度频率600Hz,色温3.2KHz;    0------亮度频率4KHz,色温16KHz

#if ZZX_Test
#define SL_PWM_CDN_PWM_LIGHTNESS_FREQUENCY       600
#define SL_PWM_CDN_PWM_CT_FREQUENCY              3200
#else
#define SL_PWM_CDN_PWM_LIGHTNESS_FREQUENCY       4000
#define SL_PWM_CDN_PWM_CT_FREQUENCY							 16000
#endif

#define SL_PWM_CDN_PWM_POLARITY_HIGH             PWM_ACTIVE_HIGH
#define SL_PWM_CDN_PWM_POLARITY_LOW              PWM_ACTIVE_LOW

#define SL_PWM_PERIPHERAL_LT                     TIMER0
#define SL_PWM_PERIPHERAL_LT_NO                  0

#define SL_PWM_PERIPHERAL_CT                     TIMER1
#define SL_PWM_PERIPHERAL_CT_NO                  0




#if CDN_MAX_POWER_12W_24W
#define CDN_24W_Max_Cur       500
#define CDN_24W_Min_Cur       350
#define CDN_24W_4_Cur       500
#define CDN_24W_3_Cur       450
#define CDN_24W_2_Cur       420
#define CDN_24W_1_Cur       350
#else
#define CDN_12W_Max_Cur       280
#define CDN_12W_Min_Cur       100
#define CDN_12W_4_Cur       280
#define CDN_12W_3_Cur       200
#define CDN_12W_2_Cur       150
#define CDN_12W_1_Cur       100
#endif


#if CDN_LIGHT_BULB
#define SL_PWM_LIGHTNESS_OUTPUT_CHANNEL          0
#define SL_PWM_LIGHTNESS_OUTPUT_PORT             gpioPortC
#define SL_PWM_LIGHTNESS_OUTPUT_PIN              4

#define SL_PWM_CT_OUTPUT_CHANNEL                 0
#define SL_PWM_CT_OUTPUT_PORT                    gpioPortD
#define SL_PWM_CT_OUTPUT_PIN                     3
#elif CDN_LIGHT_STRIP
#if CDN_STRIP_DIMMING_LIGHT
#define SL_PWM_LIGHTNESS_OUTPUT_CHANNEL          0
#define SL_PWM_LIGHTNESS_OUTPUT_PORT             gpioPortD
#define SL_PWM_LIGHTNESS_OUTPUT_PIN              3

#define SL_PWM_CT_OUTPUT_CHANNEL                 0
#define SL_PWM_CT_OUTPUT_PORT                    gpioPortD
#define SL_PWM_CT_OUTPUT_PIN                     4
#else
#define SL_PWM_LIGHTNESS_OUTPUT_CHANNEL          0
#define SL_PWM_LIGHTNESS_OUTPUT_PORT             gpioPortD
#define SL_PWM_LIGHTNESS_OUTPUT_PIN              4

#define SL_PWM_CT_OUTPUT_CHANNEL                 0
#define SL_PWM_CT_OUTPUT_PORT                    gpioPortD
#define SL_PWM_CT_OUTPUT_PIN                     3
#endif
#endif

#define CDN_PWM_DUTY                             cdn_pwm_duty
#define CDN_PWM_FREQ                             cdn_pwm_freq
#define DIM_CT_FADE_TIME                         5    //最长渐变时间5s
#define DIM_CT_MAX_TIME                          5000  //最长渐变时间5000ms

#define PWM_MAX_DUTY_CYCLE                       100  // 100% PWM duty cycle
#define PWM_MIN_DUTY_CYCLE                       0    // 0% PWM duty cycle
#define PWM_DIM_MIN_CYCLE												 1

#define false                                    0
#define true                                     1

#define PWM_DEBUG_ENABLE                         0

#define DIM_MAX_VALUE_INDEX                      1001
#define DIM_MAX_VALUE                            20000
#define LIGHT_ADJUST_INTERVAL                    10 //10ms跳变一次
#define LIGHT_ADJUST_INDEX                       ((DIM_MAX_VALUE_INDEX - 1) / (DIM_CT_MAX_TIME / LIGHT_ADJUST_INTERVAL))
#define LIGHT_ADJUST_INDEX_X2                    (LIGHT_ADJUST_INDEX * 2)
#define LIGHT_REMAIN_INTERVAL                    (DIM_CT_MAX_TIME / PWM_MAX_DUTY_CYCLE)
#define LIGHT_REMAIN_INTERVAL_HALF               (LIGHT_REMAIN_INTERVAL / 2)

/*******************************************************************************
 * function define
*******************************************************************************/
void set_pwm_timer_start_flag(uint8_t flag);
uint8_t get_pwm_timer_start_flag(void);
void set_pwm_ct_timer_start_flag(uint8_t flag);
uint8_t get_pwm_ct_timer_start_flag(void);
void set_pwm_rgb_timer_start_flag(uint8_t flag);
uint8_t get_pwm_rgb_timer_start_flag(void);
void cdn_sl_pwm_init_instances(void);
void CdnDimPwmStart(void);
void CdnCtPwmStart(void);
void GeneralLightnessCtrl(uint8_t percent);
void GeneralLightnessSetPercent(sl_pwm_instance_t *pwm, uint8_t percent);
#if CDN_LIGHT_BULB
void cdn_dim_pwm_set_frequncy(sl_pwm_instance_t *pwm, uint32_t top);
void cdn_pwm_set_duty_cycle(sl_pwm_instance_t *pwm, uint32_t duty);
void DimLightnessCtrl(uint8_t percent);
void CtLightnessCtrl(uint8_t percent);
void DimLightnessFadeCtrl(uint16_t index);
void CTLightnessFadeCtrl(uint16_t index);
void LightnessENHandle(void);
void CdnLightnessProcessActionForBulb(void);
void CdnColortempProcessActionForBulb(void);
#elif CDN_LIGHT_STRIP
void set_pwm_ct_timer_start_flag(uint8_t flag);
uint8_t get_pwm_ct_timer_start_flag(void);
void GeneralLightnessSetDutycycle(sl_pwm_instance_t *pwm, uint32_t duty);
void CdnLightStripCtrlRepeat(uint8_t Lightness,uint8_t Temp);
void CdnLightStripColorTempCtrl(uint32_t Lightness,uint32_t Temp);
void CdnStripLightnessSetIndex(uint16_t index);
void CdnStripTempSetIndex(uint16_t index);
void CdnLightnessProcessActionForStrip(void);
void CdnColortempProcessActionForStrip(void);
#endif
void CDN_Light_Breath_effect(void);
void CDN_Light_Quick_Breath_effect(void);

#endif  // CDN_PWM_H
